package com.example.yangyinzhaixian.Fragemt.record;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yangyinzhaixian.Bean.Recordbean;
import com.example.yangyinzhaixian.R;
import com.example.yangyinzhaixian.uitl.OkHttpClientManager;
import com.example.yangyinzhaixian.uitl.SpaceItemDecoration;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment2 extends Fragment {

    private Recordbean recordbean;
    private RecyclerView Fragment2_recycler;

    public BlankFragment2(Recordbean recordbean) {
        // Required empty public constructor
        this.recordbean = recordbean;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
        initView(inflate);
        List<Recordbean.DataBean.ReviewBean> review = recordbean.getData().getReview();
        if (review.size() > 0) {
            Recycler recycler = new Recycler(review);
            Fragment2_recycler.setAdapter(recycler);
        }
        return inflate;
    }

    private void initView(View inflate) {
        Fragment2_recycler = (RecyclerView) inflate.findViewById(R.id.Fragment2_recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        Fragment2_recycler.addItemDecoration(new SpaceItemDecoration(40));
        Fragment2_recycler.setLayoutManager(linearLayoutManager);
    }

    class Recycler extends RecyclerView.Adapter<Recycler.holer> {
        List<Recordbean.DataBean.ReviewBean> review;
        private ImageView retros_image;


        public Recycler(List<Recordbean.DataBean.ReviewBean> review) {
            this.review = review;
        }

        class holer extends RecyclerView.ViewHolder {
            private ImageView retros_image;
            private TextView retros_text_title;
            private TextView retros_price;
            private TextView retros_try_see;
            private TextView retros_by;

            public holer(View itemView) {
                super(itemView);
                retros_image=itemView.findViewById(R.id.retros_image);
                retros_text_title=itemView.findViewById(R.id.retros_text_title);
                retros_price=itemView.findViewById(R.id.retros_price);
                retros_try_see=itemView.findViewById(R.id.retros_try_see);
                retros_by=itemView.findViewById(R.id.retros_by);
            }
        }

        @Override
        public holer onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.retrospect, null);
            holer holer = new holer(inflate);
            return holer;
        }

        @Override
        public void onBindViewHolder(holer holder, int position) {
            OkHttpClientManager.displayImage(holder.retros_image,review.get(position).getImageUrl());
            holder.retros_text_title.setText(review.get(position).getCourseTitle());
            if (review.get(position).getPrice()==0)
            {
                holder.retros_price.setText("免费");
            }
            else
            {
                holder.retros_price.setText(review.get(position).getPrice()+"元");
            }
        }

        @Override
        public int getItemCount() {
            return review.size();
        }
    }
}
